<?php
namespace app\back\model;

use think\Model;
use \app\common\libs\WeixinTpl;

class WeixinQueue extends Model
{  
    protected $table = 'weixin_queue';

    /*
     * 注册微信成功，加入微信会员通知队列
     */
    function WeixinTplQueue($mobile, $from_id='', $to_id='') {
        $account_model = Account::where('mobile', $mobile)->find();

        $userdata = [
            'openid'=>$account_model->wx_openid,
            'nickname'=>$account_model->nickname,
            'mobile'=>$account_model->mobile,
            'money'=>$account_model->moneyRelation->amount_money,
        ];

        $json_data =  WeixinTpl::tplRegisterNotice($userdata);

        $queue_data = [
            'from_id' => $from_id,
            'to_id' => $to_id,
            'message_type' => 'weixin_tpl',
            'content' => $json_data,
        ];
        
        return WeixinQueue::create($queue_data);
    }

    /*
     * 创建微信获取用户信息模板消息
     */
    function createInfoTplForWx($opeid) {
        $account_model = $this->where('wx_openid', $opeid)->find();

        $gender = '';
        if($userinfo->getData('gender') !== 2)
            $gender = $userinfo->getData('gender') === 0 ? '女士' : '先生';
         
        $nickname = $account_model->username ? $account_model->username.$gender : $account_model->nickname;

        $last_cost_model = $account_model->recordRelation()->order('create_time desc')->find();
        if(!is_null($last_cost_model)){
            $last_cost_message = '您在本店最近一次消费在2017-10-09 13:22。欢迎您的再次光临';
        } else {
            $last_cost_message = '咿咿呀呀祝您生活愉快';
        }

        $userdata = [
            'openid'=>$account_model->wx_openid,
            'nickname'=>$nickname,
            'mobile'=>$account_model->mobile,
            'money'=>$account_model->moneyRelation->amount_money,
            'tickets'=>$account_model->ticketRelation()->where('expire_date', '>=', date('Y-m-d'))->count(),
            'level'=>'普通会员',
            'create_date'=>substr($account_model->create_time, 0, 10),
            'remark'=>$last_cost_message,
        ];

        $json_data =  WeixinTpl::tplRegisterNotice($userdata, request()->domain().'/index/member');

        $queue_data = [
            'from_id' => $from_id,
            'to_id' => $to_id,
            'message_type' => 'weixin_tpl',
            'content' => $json_data,
        ];
        
        return WeixinQueue::create($queue_data);
    }

    /*
     * 充值成功，创建微信通知模板消息
     */
    function wexinRechargeQueue($mobile, $recharge=0, $give=0) {

        $userinfo = Account::where('mobile', $mobile)->find();

        if(empty($userinfo->wx_openid))
            return true;

        $gender = '';
        if($userinfo->getData('gender') !== 2)
            $gender = $userinfo->getData('gender') === 0 ? '女士' : '先生';

        $recharge_data = [
            'nickname'       => $userinfo->username?$userinfo->username.$gender:$userinfo->nickname,
            'openid'       => $userinfo->wx_openid,
            'recharge_total' => ($recharge+$give).'.00',
            'recharge' => $recharge.'.00',
            'give' => $give.'.00',
            'total' => $userinfo->moneyRelation->amount_money,
        ];

        $json_data = WeixinTpl::tplRechargeNotice($recharge_data);

        $queue_data = [
            'to_id' => $userinfo->wx_openid,
            'message_type' => 'weixin_tpl',
            'content' => $json_data,
        ];
        
        return WeixinQueue::create($queue_data);
    }

    /*
     * 充值成功，创建微信通知模板消息
     */
    function wexinTimesRechargeQueue($mobile, $recharge_times=0) {

        $userinfo = Account::where('mobile', $mobile)->find();

        if(empty($userinfo->wx_openid))
            return true;

        $gender = $userinfo->getGenderText();

        $recharge_data = [
            'nickname'       => $userinfo->username?$userinfo->username.$gender:$userinfo->nickname,
            'openid'       => $userinfo->wx_openid,
            'mobile'       => $userinfo->mobile,
            'recharge_times' => $recharge_times,
            'times' => ($userinfo->moneyRelation->diy_times+$userinfo->moneyRelation->fish_times+$userinfo->moneyRelation->rollball_times),
        ];

        $json_data = WeixinTpl::tplTimesRechargeNotice($recharge_data);

        $queue_data = [
            'to_id' => $userinfo->wx_openid,
            'message_type' => 'weixin_tpl',
            'content' => $json_data,
        ];
        
        return WeixinQueue::create($queue_data);
    }

    /*
     * 消费成功，创建微信通知模板消息
     */
    function wexinConsumptionQueue($mobile, $cost=0, $diy_times=0, $fish_times=0, $rollball_times=0) {

        $userinfo = Account::where('mobile', $mobile)->find();

        if(empty($userinfo->wx_openid))
            return true;

        $gender = '';

        $times = ($userinfo->moneyRelation->diy_times)+($userinfo->moneyRelation->fish_times)+($userinfo->moneyRelation->rollball_times);

        if($userinfo->getData('gender') !== 2)
            $gender = $userinfo->getData('gender') === 0 ? '女士' : '先生';

        $recharge_data = [
            'nickname'       => $userinfo->username?$userinfo->username.$gender:$userinfo->nickname,
            'openid'       => $userinfo->wx_openid,
            'mobile'       => $userinfo->mobile,
            'cost' => ($cost>0?$cost.'.00元 ':'').($diy_times>0?"DIY乐园{$diy_times}次 ":'').($fish_times>0?"吃奶鱼{$fish_times}次 ":'').($rollball_times>0?"动物滚球{$rollball_times}次":''),
            'money' => $userinfo->moneyRelation->amount_money.'元 '.($times>0?$times.'次':''),
        ];

        $json_data = WeixinTpl::tplCostNotice($recharge_data);

        $queue_data = [
            'to_id' => $userinfo->wx_openid,
            'message_type' => 'weixin_tpl',
            'content' => $json_data,
        ];
        
        return WeixinQueue::create($queue_data);
    }

    /*
     * 修改会员信息，发送客服通知消息
     */
    function editKefuNotice($mobile) {
        $userinfo = Account::where('mobile', $mobile)->find();

        if(empty($userinfo->wx_openid))
            return true;

        $text = "通知：\n        管理员修改了您的帐户余额信息，<a href=\"".config('weixin.member_url')."\">点击查看详情</a>。如有疑问，请致电：".config('websys.phone_num');
        $json_data = WeixinTpl::kefuTextTpl($userinfo->wx_openid, $text);


        $queue_data = [
            'to_id' => $userinfo->wx_openid,
            'message_type' => 'weixin_kefu',
            'content' => json_encode($json_data, JSON_UNESCAPED_UNICODE),
        ];
        
        return WeixinQueue::create($queue_data);
    }
}
